<template>
  <div class="patient-profile">
    <div class="profile-header">
      <h2>个人信息</h2>
      <el-button type="primary" icon="Edit" @click="editMode = true">编辑信息</el-button>
    </div>
    
    <el-card class="profile-card">
      <el-form :model="profileForm" label-width="100px" v-if="editMode">
        <el-form-item label="用户名">
          <el-input v-model="profileForm.username" />
        </el-form-item>
        
        <el-form-item label="手机号">
          <el-input v-model="profileForm.phone" />
        </el-form-item>
        
        <el-form-item label="真实姓名">
          <el-input v-model="profileForm.realName" />
        </el-form-item>
        
        <el-form-item label="年龄">
          <el-input v-model="profileForm.age" type="number" />
        </el-form-item>
        
        <el-form-item label="身份证号">
          <el-input v-model="profileForm.idCard" />
        </el-form-item>
        
        <el-form-item label="病史">
          <el-input v-model="profileForm.medicalHistory" type="textarea" rows="3" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="saveProfile">保存</el-button>
          <el-button @click="cancelEdit">取消</el-button>
        </el-form-item>
      </el-form>
      
      <el-descriptions :column="2" border v-else>
        <el-descriptions-item label="用户名">{{ profileForm.username }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ profileForm.phone }}</el-descriptions-item>
        <el-descriptions-item label="真实姓名">{{ profileForm.realName }}</el-descriptions-item>
        <el-descriptions-item label="年龄">{{ profileForm.age }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ profileForm.idCard }}</el-descriptions-item>
        <el-descriptions-item label="病史">{{ profileForm.medicalHistory || '无' }}</el-descriptions-item>
      </el-descriptions>
    </el-card>
    
    <el-card class="health-card">
      <template #header>
        <div class="card-header">
          <h3>健康信息</h3>
          <el-button type="text" icon="Edit" @click="editHealth = true">编辑</el-button>
        </div>
      </template>
      
      <el-form :model="healthForm" label-width="100px" v-if="editHealth">
        <el-form-item label="血型">
          <el-select v-model="healthForm.bloodType" placeholder="请选择血型">
            <el-option label="A型" value="A" />
            <el-option label="B型" value="B" />
            <el-option label="O型" value="O" />
            <el-option label="AB型" value="AB" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="过敏史">
          <el-input v-model="healthForm.allergies" type="textarea" rows="2" />
        </el-form-item>
        
        <el-form-item label="慢性病史">
          <el-input v-model="healthForm.chronicDiseases" type="textarea" rows="2" />
        </el-form-item>
        
        <el-form-item label="主治医生">
          <el-input v-model="healthForm.primaryDoctor" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="saveHealth">保存</el-button>
          <el-button @click="cancelHealthEdit">取消</el-button>
        </el-form-item>
      </el-form>
      
      <el-descriptions :column="2" border v-else>
        <el-descriptions-item label="血型">{{ healthForm.bloodType || '未设置' }}</el-descriptions-item>
        <el-descriptions-item label="过敏史">{{ healthForm.allergies || '无' }}</el-descriptions-item>
        <el-descriptions-item label="慢性病史">{{ healthForm.chronicDiseases || '无' }}</el-descriptions-item>
        <el-descriptions-item label="主治医生">{{ healthForm.primaryDoctor || '未设置' }}</el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useAuthStore } from '@/store'
import { ElMessage } from 'element-plus'

const authStore = useAuthStore()
const editMode = ref(false)
const editHealth = ref(false)

const userInfo = computed(() => authStore.userInfo || {})

// 初始化表单数据
const profileForm = reactive({
  username: userInfo.value.username || '',
  phone: userInfo.value.phone || '',
  realName: userInfo.value.realName || '',
  age: userInfo.value.age || '',
  idCard: userInfo.value.idCard || '',
  medicalHistory: userInfo.value.medicalHistory || ''
})

const healthForm = reactive({
  bloodType: userInfo.value.bloodType || '',
  allergies: userInfo.value.allergies || '',
  chronicDiseases: userInfo.value.chronicDiseases || '',
  primaryDoctor: userInfo.value.primaryDoctor || ''
})

const saveProfile = () => {
  authStore.setUserInfo(profileForm)
  editMode.value = false
  ElMessage.success('个人信息保存成功')
}

const cancelEdit = () => {
  Object.assign(profileForm, {
    username: userInfo.value.username || '',
    phone: userInfo.value.phone || '',
    realName: userInfo.value.realName || '',
    age: userInfo.value.age || '',
    idCard: userInfo.value.idCard || '',
    medicalHistory: userInfo.value.medicalHistory || ''
  })
  editMode.value = false
}

const saveHealth = () => {
  authStore.setUserInfo(healthForm)
  editHealth.value = false
  ElMessage.success('健康信息保存成功')
}

const cancelHealthEdit = () => {
  Object.assign(healthForm, {
    bloodType: userInfo.value.bloodType || '',
    allergies: userInfo.value.allergies || '',
    chronicDiseases: userInfo.value.chronicDiseases || '',
    primaryDoctor: userInfo.value.primaryDoctor || ''
  })
  editHealth.value = false
}
</script>

<style scoped>
.patient-profile {
  padding: 20px;
}

.profile-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.profile-card, .health-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-descriptions {
  margin-top: 10px;
}
</style>